package com.webbei.HAO.dao.impl;

import java.io.File;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.webbei.HAO.dao.ChDao;
import com.webbei.pojo.Brands;
import com.webbei.pojo.Carseries;
import com.webbei.pojo.Cartypes;
import com.webbei.pojo.Customer;
import com.webbei.pojo.Customerlimit;
import com.webbei.pojo.LoveCar;

public class ChDaoimpl implements ChDao {
	private SqlSession session;// ���ҵ�applicationע��

	public List<Brands> list() {
		@SuppressWarnings("unchecked")
		List<Brands> list = session.selectList("Brands.getBrands");
		return list;
	}

	public List<Carseries> carlist(int id) {
		List<Carseries> list = session.selectList("Carseries.getCarseries", id);
		return list;
	}

	public List<Customer> cuslist() {
		List<Customer> list = session.selectList("customer.getUsername");
		return list;
	}
	
	public int customercheck(String email){
		return (Integer) session.selectOne("customer.getCustomerCount", email);
	} 
	
	public int customercheck2(String name){
		return (Integer) session.selectOne("customer.getCustomerCount2", name);
	} 

	public SqlSession getSession() {
		return session;
	}

	public void setSession(SqlSession session) {
		this.session = session;
	}

	public boolean update(int userid, String username, String realname,
			String file, String sex, String provaice, String city,
			String address, String telphone, String phone) {
		Customer cus = new Customer();
		cus.setUserId(userid);
		cus.setUserName(username);
		cus.setUserRealName(realname);
		cus.setUserFace(file);
		cus.setUserSex(sex);
		cus.setUserProvince(provaice);
		cus.setUserCity(city);
		cus.setUserAddress(address);
		cus.setUsertelePhone(telphone);
		cus.setUserMobilePhone(phone);
		int i = session.update("customer.updateCustomer", cus);
		if (i != 0)
			return true;
		else
			return false;
	}

	public void createfile(String userpath) {
		File file = new File(userpath);
		if (!file.exists()) {
			file.mkdirs();
		}
		File f1=new File(userpath+"/userTouxiang");
		if(!f1.exists()){
			f1.mkdirs();
		}
		File f2=new File(userpath+"/userLovecar");
		if(!f2.exists()){
			f2.mkdirs();
		}
		/*File f3=new File(userpath+"/userOrder");
		if(!f3.exists()){
			f3.mkdirs();
		}*/
		
		File f4=new File(userpath+"/userCustom");
		if(!f4.exists()){
			f4.mkdirs();
		}
		File f5=new File(userpath+"/userCDR");
		if(!f5.exists()){
			f5.mkdirs();
		}
	}

	public int getUidbyEmail(String mail) {
		int uid = (Integer) session.selectOne("customer.getUid", mail);
		return uid;
	}

	/*public boolean insertUname(String uname, int id, String userface) {

		Customer cus = new Customer();
		cus.setUserName(uname);
		cus.setUserId(id);
		cus.setUserFace(userface);
		int i = session.update("customer.updateuname", cus);
		if (i != 0)
			return true;
		else
			return false;
	}*/

	@Override
	public String getMailbyUid(int id) {
		String mail = (String) session.selectOne("customer.getMail", id);
		return mail;
	}

	@Override
	public Customer cus(int id) {
		Customer cu = (Customer) session.selectOne("Carseries.getCustomer", id);
		return cu;
	}

	@Override
	public boolean updatePwdbyEmail(String mail, String pwd) {
		Customer cus = new Customer();
		cus.setUserPwd(pwd);
		cus.setUserMail(mail);
		int i = session.update("customer.resetpwd", cus);
		if (i != 0)
			return true;
		else
			return false;
	}

	@Override
	public String getPwdbyUid(int id) {
		String pwd = (String) session.selectOne("customer.getPwd", id);
		return pwd;
	}

	@Override
	public boolean updatePwdbyId(int id, String pwd) {
		Customer cus = new Customer();
		cus.setUserPwd(pwd);
		cus.setUserId(id);
		int i = session.update("customer.updatepwd2", cus);
		if (i != 0)
			return true;
		else
			return false;

	}

	@Override
	public boolean register(String mail, String password,String uname,String userface) {
		System.out.println("uname:"+uname);
		Customer cus = new Customer();
		cus.setUserMail(mail);
		cus.setUserPwd(password);
		cus.setUserName(uname);
		cus.setUserFace(userface);
		int i = session.insert("customer.registcus", cus);
		if (i != 0)
			return true;
		else
			return false;
	}
	
	public String getPwdByMail(String mail){
		return (String) session.selectOne("customer.getPwdbyMail",mail);
	}

	
	public List<Cartypes> cartypeNamelist(String seriesName) {
	
		System.out.println(seriesName);
		List<Cartypes> list=session.selectList("cartype.gettypesPic",seriesName);		
	/*  for(int i=0;i<list.size();i++){
		 System.out.println(list.get(i));
	 }*/
	     System.out.println(list);
	     return list;
		
	}

	@Override
	public int insertcuslimit(String uname) {
		Customerlimit limi=new Customerlimit();
		limi.setUserName(uname);
		return session.insert("cuslimit.insertall", limi);
	}

	@Override
	public int insertLovercar(int userid, String carPic) {
		LoveCar love=new LoveCar();
		love.setUsersId(userid);
		love.setUsersCarPic(carPic);
		return session.insert("lovecars.insertUseridAndCarpic",love);
	}

	@Override
	public LoveCar getOnecus() {
	return (LoveCar) session.selectOne("lovecars.getLoveCarsByCustomersID");
	}

	
	
}
